package cn.edu.zucc.online_market.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.online_market.itf.IProductClassManager;
import cn.edu.zucc.online_market.model.BeanProductClass;
import cn.edu.zucc.online_market.model.BeanUser;
import cn.edu.zucc.online_market.util.BaseException;
import cn.edu.zucc.online_market.util.DBPool;
import cn.edu.zucc.online_market.util.DbException;


public class ProductClassManager implements IProductClassManager {

	@Override
	public List<BeanProductClass> loadAll() throws BaseException {
		List<BeanProductClass> product_class_list= new ArrayList<BeanProductClass>();
		BeanProductClass p = null;
		Connection conn = null;
		if(BeanUser.currentLoginUser==null) BeanUser.currentLoginUser = new UserManager().login("111", "111");
		try {
			conn = DBPool.getInstance().getConnection();
			String sql;
			java.sql.PreparedStatement pst;
			java.sql.ResultSet rs;
			
			sql = "select class_id,class_name,class_description from product_class ;";
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()) {
				p = new BeanProductClass();
				p.setProduct_class_id(rs.getInt(1));
				p.setProduct_class_name(rs.getString(2));
				p.setProduct_class_description(rs.getString(3));
				product_class_list.add(p);
			}
			pst.close();
			rs.close();	
		}catch(SQLException e) {
			e.printStackTrace();
			throw new DbException(e);
		}finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return product_class_list;
	}

}
